﻿using System.Data;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ExcelExport
{
    public class GenerateJson
    {
        private StringBuilder jsonBuilder = new StringBuilder();

        public void WriteToFile(string className, List<TableField> fieldList, DataTable dt )
        {
            Release();
            int rowsCount = dt.Rows.Count;
            int columnsCount = dt.Columns.Count;
            //遍历数据
            for (int i = 0; i < rowsCount; i++)
            {
                for (int j = 0; j < columnsCount; j++)
                {
                    TableField field = fieldList[j];
                    object o = dt.Rows[i][j];
                    string objectStr = o.ToString();
                    JsonCode(field, objectStr);
                }
                NewLine(i >= rowsCount - 1);
            }
            CreateJsonFile(className);
        }

        private void JsonCode(TableField field, string str)
        {
            if (!field.isList)
            {
                switch (field.fieldType)
                {
                    case TableType.BYTE:
                    case TableType.SHORT:
                    case TableType.INT:
                    case TableType.ULONG:
                    case TableType.FLOAT:
                        AddJsonData(field.filedName, str);
                        break;
                    case TableType.BOOL:
                        if (str.Equals("0"))
                        {
                            str = "false";
                        }
                        if (str.Equals("1"))
                        {
                            str = "true";
                        }
                        AddJsonData(field.filedName, str);
                        break;
                    case TableType.STRING:
                        str = str.Replace("\n", "");
                        str = str.Replace(Util.DATA_SEP, "");
                        str = JsonConvert.SerializeObject(str);
                        AddJsonData(field.filedName, str);
                        break;
                }
            }
            else
            {
                if (!str.Equals(Util.NULL))
                {
                    switch (field.fieldType)
                    {
                        case TableType.BYTE:
                        case TableType.SHORT:
                        case TableType.INT:
                            int[] bArr = Array.ConvertAll<string, int>(str.Split('~'), s => int.Parse(s));
                            AddJsonData(field.filedName, bArr);
                            break;
                        case TableType.FLOAT:
                            float[] fArr = Array.ConvertAll<string, float>(str.Split('~'), s => float.Parse(s));
                            AddJsonData(field.filedName, fArr);
                            break;
                        case TableType.BOOL:
                            string[] boolSpl = str.Split('~');
                            bool[] bools = new bool[boolSpl.Length];
                            for (int i = 0; i < boolSpl.Length; i++)
                            {
                                if (boolSpl[i].Equals("0"))
                                {
                                    str = "false";
                                }
                                if (boolSpl[i].Equals("1"))
                                {
                                    str = "true";
                                }
                                bools[i] = bool.Parse(boolSpl[i]);
                            }
                            AddJsonData(field.filedName, bools.ToString());
                            break;
                        case TableType.STRING:
                            str = str.Replace("\n", "");
                            str = str.Replace(Util.DATA_SEP, "");
                            string[] strSpl = str.Split('~');
                            AddJsonData(field.filedName, strSpl);
                            break;
                    }
                }
            }
        }

        private void AddJsonData(string fieldName, object val)
        {
            string key = JsonConvert.SerializeObject(fieldName);
            jsonBuilder.Append(key);
            jsonBuilder.Append(":");
            jsonBuilder.Append(val);
            jsonBuilder.Append(",");
        }

        private void NewLine(bool isLastLine)
        {
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
            if (isLastLine)
            {
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
            }
            else
            {
                jsonBuilder.Append("\n");
                jsonBuilder.Append("{");
            }
        }

        private void Release()
        {
            jsonBuilder.Clear();
            jsonBuilder.Append("[{");
        }

        private void CreateJsonFile(string className)
        {
            Utility.WriteFile(Util.dirOutJson + "/" + className + ".json", jsonBuilder.ToString());
        }
    }
}
